package com.sl.au.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.sl.au.entity.AfterSaleOutHouse;
import com.sl.au.entity.AfterSaleTicket;
import com.sl.au.entity.AfterSaleTicketTrance;
import com.sl.au.repository.AfterSaleOutHouseRepository;
import com.sl.au.repository.AfterSaleTicketTranceRepository;
import com.sl.au.service.AfterSaleOutHouseService;

@Service("AfterSaleOutHouseService")
public class AfterSaleOutHouseServiceImpl extends BaseServiceImpl implements AfterSaleOutHouseService {

	@Autowired
	AfterSaleOutHouseRepository afterSaleOutHouseRepository;
	@Autowired
	AfterSaleTicketTranceRepository afterSaleTicketTranceRepository;

	@Override
	public AfterSaleOutHouseRepository getRepository() {
		// TODO Auto-generated method stub
		return this.afterSaleOutHouseRepository;
	}

	@Override
	public List<AfterSaleOutHouse> getOutHouseTicket(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleOutHouse> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleOutHouseRepository.findOutHouseTicket(condition.get("userId"), condition.get("key"), pageable);
		return pages.getContent();
	}

	@Override
	public List<AfterSaleOutHouse> getMyOutHouseTicket(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleOutHouse> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		pages = afterSaleOutHouseRepository.findMyOutHouseTicket(condition.get("userId"), condition.get("key"),
				condition.get("outState"),pageable);
		return pages.getContent();
	}

	@Override
	public AfterSaleTicketTranceRepository getTicketTranceRepository() {
		// TODO Auto-generated method stub
		return this.afterSaleTicketTranceRepository;
	}

	@Override
	public List<AfterSaleTicketTrance> getTicketTrance(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleTicketTrance> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		pages = afterSaleOutHouseRepository.findTicketTrance(condition.get("userId"), condition.get("ticketId"),
				pageable);
		return pages.getContent();
	}

	@Override
	public List<AfterSaleOutHouse> getTicketTrancePerson(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleOutHouse> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "updateTime");
		pages = afterSaleOutHouseRepository.findTicketTrancePerson(condition.get("ticketId"), pageable);
		return pages.getContent();
	}

	@Override
	public int getTicketFinishState(String ticketId) {
		List<AfterSaleOutHouse> outs = afterSaleOutHouseRepository.findAllOuts(ticketId);
		for (AfterSaleOutHouse a : outs) {
			String state = a.getOutState();
			if (state == null) {
				state = "0";
			}
			if (!state.equals("3")) {
				return 1;
			}
		}
		return 0;
	}

	@Override
	public List<AfterSaleTicketTrance> findAllTrance(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleTicketTrance> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		String outId = condition.get("outId");
		pages = afterSaleOutHouseRepository.findAllTrance(outId, pageable);
		return pages.getContent();
	}

	@Override
	public List<AfterSaleOutHouse> findAllNoRecivedOuts(int start, int length, String ticketId) {
		Page<AfterSaleOutHouse> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleOutHouseRepository.findAllNoReceivedOuts(ticketId, pageable);
		return pages.getContent();
	}

	@Override
	public List<AfterSaleOutHouse> getAllOutHouseTicket(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleOutHouse> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleOutHouseRepository.getAllOutHouseTicket(condition.get("key"), pageable);
		return pages.getContent();
	}

	@Override
	public List<AfterSaleTicketTrance> findAllTranceBykey(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleTicketTrance> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		String outId = condition.get("outId");
		String key = condition.get("key");
		pages = afterSaleOutHouseRepository.findAllTranceBykey(outId, key, pageable);
		return pages.getContent();
	}

}
